package com.zn.kjj.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * json处理的工具类，如json串转json对象、json对象与java对象的转化等功能
 * @author hejw 2014年5月5日9:33:40
 */
public class JSONTool {
    /**
     * 取json中普通字符串，防止没有此key造成抛出异常报错
     * @param jsonObj .
     * @param key 字符串
     * @return 字符串
     */
    public static String getStrJsonObj(JSONObject jsonObj,String key){
        String value = "";
        try {
            value = jsonObj.getString(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    /**
     * 把json字符串转化为json对象
     * @param jsonStr 入参json字符串
     * @return jSONObject
     */
    public static JSONObject getJsonStr(String jsonStr) {
        JSONObject jSONObject = null;
        try {
            jSONObject = JSONObject.fromObject(jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jSONObject;
    }

    /**
     * json字符串转化为 HashMap<String ,String>
     * @param jsonStr .
     * @return 集合
     */
    public static HashMap<String, String> getMapByStr(String jsonStr) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        if(jsonStr==null){
            return hashMap;
        }
        JSONObject jSONObject = getJsonStr(jsonStr);
        if (jSONObject != null) {
            @SuppressWarnings("unchecked")
            Iterator<String> it = jSONObject.keys();
            while (it.hasNext()) {
                String key = String.valueOf( it.next());
                String value = jSONObject.getString(key);
                // System.out.println("key = "+key+"   value = "+value);
                hashMap.put(key, value);
            }
        }
        return hashMap;
    }
    /**
     * json字符串转化为 HashMap<String ,String>
     * @param jsonStr .
     * @return 集合
     */
    public static HashMap<String, Object> getMapJsonStr(String jsonStr) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        if(jsonStr==null){
            return hashMap;
        }
        JSONObject jSONObject = getJsonStr(jsonStr);
        if (jSONObject != null) {
            @SuppressWarnings("unchecked")
            Iterator<String> it = jSONObject.keys();
            while (it.hasNext()) {
                String key = String.valueOf( it.next());
                String value = jSONObject.getString(key);
                // System.out.println("key = "+key+"   value = "+value);
                hashMap.put(key, value);
            }
        }
        return hashMap;
    }
    /**
     * json对象转化为 HashMap<String ,String>
     * @param jSONObject .
     * @return 集合
     */
    public static HashMap<String, String> getMapJsonObj(JSONObject jSONObject) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        if(jSONObject==null) {
            return hashMap;
        }
        if (jSONObject != null) {
            @SuppressWarnings("unchecked")
            Iterator<String> it = jSONObject.keys();
            while (it.hasNext()) {
                String key = String.valueOf( it.next());
                String value = jSONObject.getString(key);
                // System.out.println("key = "+key+"   value = "+value);
                hashMap.put(key, value);
            }
        }
        return hashMap;
    }
    /**
     * json对象转化为 HashMap<String ,Object>
     * @param jSONObject .
     * @return 集合
     */
    public static HashMap<String, Object> getMap2JsonObj(JSONObject jSONObject) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        if(jSONObject==null) {
            return hashMap;
        }
        if (jSONObject != null) {
            @SuppressWarnings("unchecked")
            Iterator<String> it = jSONObject.keys();
            while (it.hasNext()) {
                String key = String.valueOf( it.next());
                String value = jSONObject.getString(key);
                // System.out.println("key = "+key+"   value = "+value);
                hashMap.put(key, value);
            }
        }
        return hashMap;
    }
    /**
     * json数组对象串转为 ListMap
     * @param jsonArray json数组
     * @return 集合
     */
    public static List<Map<String, Object>> getListMapJsonArrObj(JSONArray jsonArray) {
        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        if(jsonArray==null){
            return listmap;
        }
        try {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                listmap.add(getMapJsonStr(jsonObject.toString()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listmap;
    }
    /**
     * 将HashMap<String ,String> 转化为json字符串
     * @param map 集合
     * @return 字符串
     */
    public static String getJsonStrMap(HashMap<String, String> map) {
        String jsonStr = "{}"; // 空的json串
        if(map==null){
            return jsonStr;
        }
        try {
            JSONObject jsonObject = new JSONObject();
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String key = String.valueOf( it.next());
                String value = String.valueOf( map.get(key));
                jsonObject.put(key, value);
            }
            jsonStr = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonStr;
    }
    /**
     * 将HashMap<String ,String> 转化为json字符串
     * @param map 集合
     * @return 字符串
     */
    public static String getJsonStrMap4Obj(HashMap<String, Object> map) {
        String jsonStr = "{}"; // 空的json串
        if(map==null){
            return jsonStr;
        }
        try {
            JSONObject jsonObject = new JSONObject();
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String key = String.valueOf( it.next());
                String value = String.valueOf( map.get(key));
                jsonObject.put(key, value);
            }
            jsonStr = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    /**
     * listmap转化为json数组串
     * @param listmap 集合
     * @return 字符串
     */
    public static String getJsonStrListMap(ArrayList<HashMap<String, String>> listmap) {
        String jsonStr = "{}"; // 空的json串
        if(listmap==null){
            return jsonStr;
        }
        try {
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            for (int i = 0; i < listmap.size(); i++) {
                sb.append(getJsonStrMap(listmap.get(i)));
                if (i < listmap.size() - 1) {
                    sb.append(",");
                }
            }
            sb.append("]");
            jsonStr = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonStr;

    }

    /**
     * 将java对象转化为json字符串
     * @param benObj object
     * @return 字符串
     */
    public static String getJsonStrBean(Object benObj) {
        String jsonStr = "{}"; // 空的json串
        if(benObj==null){
            return jsonStr;
        }
        try {
            JSONObject jSONObject = JSONObject.fromObject(benObj);
            jsonStr = jSONObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    /**
     * List<Object> 对象转化为json字符串
     * @param listBean 集合
     * @return 字符串
     */
    public static String getJsonStrListBean(ArrayList<Object> listBean) {
        String jsonStr = "{}"; // 空的json串
        if(listBean==null){
            return jsonStr;
        }
        try {
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            for (int i = 0; i < listBean.size(); i++) {
                sb.append(getJsonStrBean(listBean.get(i)));
                if (i < listBean.size() - 1) {
                    sb.append(",");
                }
            }
            sb.append("]");
            jsonStr = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    /**
     * json字符串转化为bean对象
     * @param jsonStr 字符串
     * @param cls .
     * @return object
     */
    @SuppressWarnings({ "static-access", "rawtypes" })
    public static Object getBeanJsonStr(String jsonStr, Class cls) {
        Object object = null;
        try {
            JSONObject jSONObject = getJsonStr(jsonStr);
            object = JSONObject.toBean(jSONObject, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    /**
     * json字符串转化为ArrayList<Bean>对象
     * @param jsonStr 字符串
     * @param cls .
     * @return 集合
     */
    @SuppressWarnings("rawtypes")
    public static ArrayList<Object> getListBeanJsonStr(String jsonStr, Class cls) {
        ArrayList<Object> listBean = new ArrayList<Object>();
        try {
            JSONArray jsonArray = JSONArray.fromObject(jsonStr);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                listBean.add(getBeanJsonStr(jsonObject.toString(), cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listBean;
    }

}
